package cn.jiyun.wab;


import cn.jiyun.zhifubao.AppUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Controller
public class PayController {

    @Resource
    private AlipayClient alipayClient;
    @Resource
    private AlipayTradePagePayRequest alipayTradePagePayRequest;
    //处理支付请求
    @RequestMapping("/pay")
    public void pay(String WIDout_trade_no, String WIDsubject, String WIDtotal_amount, String WIDbody, HttpServletResponse res) throws Exception{
        System.out.println("1111111");
        //2.获得支付的客户端AlipayClient,和配置支付信息的对象AlipayTradePagePayRequest
        //3.设置相应的地址（支付宝返回给客户端的地址）
        alipayTradePagePayRequest.setNotifyUrl(AppUtil.notify_url);
        alipayTradePagePayRequest.setReturnUrl(AppUtil.return_url);
        //4.设置请求的参数（传递给支付宝的数据）
        alipayTradePagePayRequest.setBizContent(
                "{\"out_trade_no\":\""+WIDout_trade_no+"\","
                + "\"total_amount\":\""+WIDtotal_amount+"\","
                + "\"subject\":\""+WIDsubject+"\","
                + "\"body\":\""+WIDbody+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //5.发送请求
        String result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        //6.将相应结果返回给客户端
        res.setContentType("text/html;charset=utf-8");
        res.getWriter().write(result);
    }

    @RequestMapping("/pay2")
    public void pay2(String trade_no,String WIDsubject, String WIDtotal_amount, String WIDbody, HttpServletResponse res) throws Exception{
        System.out.println("1111111");
        //2.获得支付的客户端AlipayClient,和配置支付信息的对象AlipayTradePagePayRequest
        //3.设置相应的地址（支付宝返回给客户端的地址）
        alipayTradePagePayRequest.setNotifyUrl(AppUtil.notify_url);
        alipayTradePagePayRequest.setReturnUrl(AppUtil.return_url);
        //4.设置请求的参数（传递给支付宝的数据）
        alipayTradePagePayRequest.setBizContent(
                "{\"out_trade_no\":\""+trade_no+"\","
                        + "\"total_amount\":\""+WIDtotal_amount+"\","
                        + "\"subject\":\""+WIDsubject+"\","
                        + "\"body\":\""+WIDbody+"\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //5.发送请求
        String result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        //6.将相应结果返回给客户端
        res.setContentType("text/html;charset=utf-8");
        res.getWriter().write(result);
    }
}
